/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Inquiry;
import org.cx1.model.Listing;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IInquiryService;
import org.cx1.service.impl.MInquiryService;
import org.cx1.web.controller.action.HomeAction;
import org.cx1.web.controller.action.InquiryAction;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class InquiryController extends BaseController {
    
    private IInquiryService inquiryService = new MInquiryService();
    
    public InquiryController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        boolean loggedin = false;
        String userID = SessionUtils.getUserID(request.getSession());   
        
        if (!StringUtils.isEmpty(userID)) {
            loggedin = true;
            map.put("loggedin", loggedin);
        }
        
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            case InquiryAction.VIEW_TOUR_SPACE_INQUIRIES: {                
                if (loggedin) {
                    viewTourSpaceInquiries(mav, map, request);               
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            default: {
                if (loggedin) {
                    viewTourSpaceInquiries(mav, map, request);               
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
        }        
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        
        mav.addObject("model", map);
        return mav;
    }
    
    private void viewTourSpaceInquiries(ModelAndView mav, Map map, HttpServletRequest request) {
        ServiceResponse<List<Inquiry>> response = inquiryService.readAllTourSpaceInquiries(0, 0);        
        map.put("inquiries", response.getResponse());
    }
    
}